﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace New3DDemo
{
    /// <summary>
    /// 隔离带
    /// </summary>
    public partial class IsolationBelt : Canvas
    {
        public IsolationBelt()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region CLR属性

        public PointCollection Points1 { get; set; }
        public PointCollection Points2 { get; set; }
        #endregion

        #region 注册依赖属性

        #region OriginPoints

        public PointCollection OriginPoints
        {
            get { return (PointCollection)GetValue(OriginPointsProperty); }
            set { SetValue(OriginPointsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OriginPoints.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OriginPointsProperty =
            DependencyProperty.Register("OriginPoints", typeof(PointCollection), typeof(IsolationBelt), new UIPropertyMetadata(new PropertyChangedCallback(OriginPointsPropertyChanged)));

        private static void OriginPointsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IsolationBelt element = (IsolationBelt)d;
            if (element != null && element.MiddleLinePoints != null)
                element.CalMiddleLinePoints();
        } 

        #endregion

        #region MiddleLinePoints

        /// <summary>
        ///构成 IsolationBelt中分线的点集合
        /// </summary>
        public PointCollection MiddleLinePoints
        {
            get { return (PointCollection)GetValue(PointsListProperty); }
            set { SetValue(PointsListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PointsList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PointsListProperty =
            DependencyProperty.Register("MiddleLinePoints", typeof(PointCollection), typeof(IsolationBelt), new UIPropertyMetadata(new PropertyChangedCallback(PropertyChanged)));

        #endregion

        #region BeltWidth

        /// <summary>
        /// 隔离带的宽度
        /// </summary>
        public double BeltWidth
        {
            get { return (double)GetValue(BeltWidthProperty); }
            set { SetValue(BeltWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BeltWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BeltWidthProperty =
            DependencyProperty.Register("BeltWidth", typeof(double), typeof(IsolationBelt), new UIPropertyMetadata(5.0,new PropertyChangedCallback(PropertyChanged)));
        
        #endregion

        #region Gap

        /// <summary>
        /// 隔离带间隙宽度
        /// </summary>
        public double Gap
        {
            get { return (double)GetValue(GapProperty); }
            set { SetValue(GapProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Gap.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GapProperty =
            DependencyProperty.Register("Gap", typeof(double), typeof(IsolationBelt), new UIPropertyMetadata(3.0, new PropertyChangedCallback(PropertyChanged)));
        
        #endregion
       
        #endregion

        #region 方法

        private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IsolationBelt element = (IsolationBelt)d;
            if (element != null && element.MiddleLinePoints != null)
                element.Refresh();
        }

        void CalMiddleLinePoints()
        {
            MiddleLinePoints = OriginPoints;
        }

        void Refresh()
        {
            if (Gap < BeltWidth)
            {
                Points1 = new PointCollection(LineHelper.MakeParallelLine(MiddleLinePoints.ToList(), (BeltWidth - Gap) / 2));
                Points2 = new PointCollection(LineHelper.MakeParallelLine(MiddleLinePoints.ToList(), -(BeltWidth - Gap) / 2));
            }
        }

        #endregion
    }
}
